package com.iplant.aps.serviceimpl.dao.aps;

import com.iplant.aps.service.mesenum.MESDBSource;
import com.iplant.aps.service.mesenum.MESException;
import com.iplant.aps.service.po.OutResult;
import com.iplant.aps.service.po.ServiceResult;
import com.iplant.aps.service.po.aps.APSPartPointWeekPlan;
import com.iplant.aps.service.po.bms.BMSEmployee;
import com.iplant.aps.service.utils.StringUtils;
import com.iplant.aps.serviceimpl.dao.BaseDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

import java.text.MessageFormat;
import java.util.*;

public class APSPartPointWeekPlanDAO extends BaseDAO {

    private static final Logger logger = LoggerFactory.getLogger(APSPartPointWeekPlanDAO.class);

    private static APSPartPointWeekPlanDAO Instance = null;

    private APSPartPointWeekPlanDAO() {
        super();
    }

    public static APSPartPointWeekPlanDAO getInstance() {
        if (Instance == null)
            Instance = new APSPartPointWeekPlanDAO();
        return Instance;
    }

    /**
     * 添加或修改
     */
    public int Update(BMSEmployee wLoginUser, APSPartPointWeekPlan wAPSPartPointWeekPlan,
                      OutResult<Integer> wErrorCode) {
        int wResult = 0;
        try {
            ServiceResult<String> wInstance = this.GetDataBaseName(wLoginUser.getCompanyID(), MESDBSource.Basic,
                    wLoginUser.getID(), 0);
            wErrorCode.set(wInstance.ErrorCode);
            if (wErrorCode.Result != 0) {
                return wResult;
            }

            if (wAPSPartPointWeekPlan == null)
                return 0;

            String wSQL = "";
            if (wAPSPartPointWeekPlan.getID() <= 0) {
                wSQL = MessageFormat.format("INSERT INTO {0}.aps_partpointweekplan(OrderID,PartID,PartPointID," +
                                "CreateID,CreateTime) VALUES(:OrderID,:PartID,:PartPointID,:CreateID,:CreateTime);",
                        wInstance.Result);
            } else {
                wSQL = MessageFormat.format("UPDATE {0}.aps_partpointweekplan SET OrderID = :OrderID,PartID = " +
                                ":PartID,PartPointID = :PartPointID,CreateID = :CreateID,CreateTime = :CreateTime " +
                                "WHERE ID = :ID;",
                        wInstance.Result);
            }

            wSQL = this.DMLChange(wSQL);

            Map<String, Object> wParamMap = new HashMap<String, Object>();

            wParamMap.put("ID", wAPSPartPointWeekPlan.ID);
            wParamMap.put("OrderID", wAPSPartPointWeekPlan.OrderID);
            wParamMap.put("PartID", wAPSPartPointWeekPlan.PartID);
            wParamMap.put("PartPointID", wAPSPartPointWeekPlan.PartPointID);
            wParamMap.put("CreateID", wAPSPartPointWeekPlan.CreateID);
            wParamMap.put("CreateTime", wAPSPartPointWeekPlan.CreateTime);

            KeyHolder keyHolder = new GeneratedKeyHolder();
            SqlParameterSource wSqlParameterSource = new MapSqlParameterSource(wParamMap);

            shrisJdbcTemplate.update(wSQL, wSqlParameterSource, keyHolder);

            if (wAPSPartPointWeekPlan.getID() <= 0) {
                wResult = Objects.requireNonNull(keyHolder.getKey()).intValue();
                wAPSPartPointWeekPlan.setID(wResult);
            } else {
                wResult = wAPSPartPointWeekPlan.getID();
            }
        } catch (Exception ex) {
            wErrorCode.set(MESException.DBSQL.getValue());
            logger.error(ex.toString());
        }
        return wResult;
    }

    /**
     * 删除集合
     */
    public ServiceResult<Integer> DeleteList(BMSEmployee wLoginUser, List<APSPartPointWeekPlan> wList,
                                             OutResult<Integer> wErrorCode) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            ServiceResult<String> wInstance = this.GetDataBaseName(wLoginUser.getCompanyID(), MESDBSource.Basic,
                    wLoginUser.getID(), 0);
            wErrorCode.set(wInstance.ErrorCode);
            if (wErrorCode.Result != 0) {
                return wResult;
            }

            if (wList == null || wList.size() <= 0)
                return wResult;

            List<String> wIDList = new ArrayList<String>();
            for (APSPartPointWeekPlan wItem : wList) {
                wIDList.add(String.valueOf(wItem.ID));
            }
            String wSql = MessageFormat.format("delete from {1}.aps_partpointweekplan WHERE ID IN({0}) ;",
                    String.join(",", wIDList), wInstance.Result);
            this.ExecuteSqlTransaction(wSql);
        } catch (Exception ex) {
            wErrorCode.set(MESException.DBSQL.getValue());
            logger.error(ex.toString());
        }
        return wResult;
    }

    /**
     * 查单条
     */
    public APSPartPointWeekPlan SelectByID(BMSEmployee wLoginUser, int wID, OutResult<Integer> wErrorCode) {
        APSPartPointWeekPlan wResult = new APSPartPointWeekPlan();
        try {
            ServiceResult<String> wInstance = this.GetDataBaseName(wLoginUser.getCompanyID(), MESDBSource.Basic,
                    wLoginUser.getID(), 0);
            wErrorCode.set(wInstance.ErrorCode);
            if (wErrorCode.Result != 0) {
                return wResult;
            }

            List<APSPartPointWeekPlan> wList = SelectList(wLoginUser, wID, -1, -1, -1, wErrorCode);
            if (wList == null || wList.size() != 1)
                return wResult;
            wResult = wList.get(0);
        } catch (Exception e) {
            wErrorCode.set(MESException.DBSQL.getValue());
            logger.error(e.toString());
        }
        return wResult;
    }

    /**
     * 条件查询集合
     */
    public List<APSPartPointWeekPlan> SelectList(BMSEmployee wLoginUser, int wID, int wOrderID, int wPartID,
                                                 int wPartPointID, OutResult<Integer> wErrorCode) {
        List<APSPartPointWeekPlan> wResultList = new ArrayList<APSPartPointWeekPlan>();
        try {
            ServiceResult<String> wInstance = this.GetDataBaseName(wLoginUser.getCompanyID(), MESDBSource.Basic,
                    wLoginUser.getID(), 0);
            wErrorCode.set(wInstance.ErrorCode);
            if (wErrorCode.Result != 0) {
                return wResultList;
            }

            String wSQL = MessageFormat.format(
                    "SELECT * FROM {0}.aps_partpointweekplan WHERE  1=1  and ( :wID <= 0 or :wID = ID ) and ( " +
                            ":wOrderID <= 0 or :wOrderID = OrderID ) and ( :wPartID <= 0 or :wPartID = PartID ) and (" +
                            " :wPartPointID <= 0 or :wPartPointID = PartPointID );",
                    wInstance.Result);

            Map<String, Object> wParamMap = new HashMap<String, Object>();

            wParamMap.put("wID", wID);
            wParamMap.put("wOrderID", wOrderID);
            wParamMap.put("wPartID", wPartID);
            wParamMap.put("wPartPointID", wPartPointID);

            wSQL = this.DMLChange(wSQL);

            List<Map<String, Object>> wQueryResult = shrisJdbcTemplate.queryForList(wSQL, wParamMap);

            for (Map<String, Object> wReader : wQueryResult) {
                APSPartPointWeekPlan wItem = new APSPartPointWeekPlan();

                wItem.ID = StringUtils.parseInt(wReader.get("ID"));
                wItem.OrderID = StringUtils.parseInt(wReader.get("OrderID"));
                wItem.PartID = StringUtils.parseInt(wReader.get("PartID"));
                wItem.PartPointID = StringUtils.parseInt(wReader.get("PartPointID"));
                wItem.CreateID = StringUtils.parseInt(wReader.get("CreateID"));
                wItem.CreateTime = StringUtils.parseCalendar(wReader.get("CreateTime"));

                wResultList.add(wItem);
            }
        } catch (Exception ex) {
            wErrorCode.set(MESException.DBSQL.getValue());
            logger.error(ex.toString());
        }
        return wResultList;
    }
}
